<?php

namespace App\Repositories\Impl;

use App\Models\MeetDay;
use App\Repositories\Contracts\MeetDayRepository;
use App\Repositories\Transformers\MeetDayListTransformer;

class MeetDayRepositoryImpl implements MeetDayRepository
{
    public function paginate($perPage, $filter)
    {
        $result = MeetDay::paginate($perPage);
        return $result;
    }

    public function find($id, $columns = ['*'])
    {
        return MeetDay::find($id, $columns);
    }

    public function findByFilter($filter, $columns = ['*'])
    {
        $meetDay = MeetDay::where('day', '>=', $filter['start_day'])
            ->where('day', '<=', $filter['end_day'])->where('venue_id', $filter['venue_id'])->with(['hours' => function ($query) use ($filter) {
            return $query->where('venue_id', $filter['venue_id']);
        }])->get();

        $result = fractal($meetDay, new MeetDayListTransformer());

        return $result->toArray();
    }

    public function store($data)
    {
        return MeetDay::create($data);
    }

    public function update($data, $id)
    {
        return MeetDay::whereId($id)->updateOrCreate($data);
    }

    public function updateByFilter($data, $where)
    {
        return MeetDay::where($where)->update($data);
    }

    public function delete($id)
    {
        return MeetDay::whereId($id)->delete();
    }
}
